package com.weyoung.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weyoung.admin.dto.req.*;
import com.weyoung.admin.mapper.FamilyMapper;
import com.weyoung.admin.mapper.UserDetailMapper;
import com.weyoung.admin.mapper.UserMapper;
import com.weyoung.admin.service.DisplayService;
import com.weyoung.common.model.entity.FamilyDO;
import com.weyoung.common.model.entity.UserDO;
import com.weyoung.common.model.entity.UserDetailDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DisplayServiceImpl extends ServiceImpl<UserMapper, UserDO> implements DisplayService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserDetailMapper userDetailMapper;

    @Autowired
    FamilyMapper familyMapper;

    @Override
    public Long getCountByTimes(Date startTime, Date endTime) {
        //获取初始日期时的用户总数
        Long startCount = userMapper.getCountByBeforeTime(startTime);
        //获取截止日期时的用户总数
        Long endCount = userMapper.getCountByBeforeTime(endTime);
        //两个日期用户总数的差值就是时间段内的新增用户数量
        return endCount - startCount;
    }

    @Override
    public Long getAgeByTimes(Date startTime, Date endTime) {
        //通过出生日期来实现对年龄段的查询
        return userDetailMapper.getAgeCountByTimes(startTime, endTime);
    }

    @Override
    public Long getCountByAddress(String address) {
        return userDetailMapper.getAgeCountByAddress(address);
    }

    @Override
    public Long getFamilyCount() {
        return familyMapper.getFamilyCount();
    }

    @Override
    public Long getFamilyCountByAddress(String family_address) {
        QueryWrapper<FamilyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(FamilyDO::getFamily_address, family_address);
        return familyMapper.selectCount(queryWrapper);
    }

    @Override
    public Long getManCount() {
       QueryWrapper<UserDetailDO> queryWrapper = new QueryWrapper<>();
       queryWrapper.lambda().eq(UserDetailDO::getGender, "male");
       return userDetailMapper.selectCount(queryWrapper);
    }

    @Override
    public Long getWomanCount() {
        QueryWrapper<UserDetailDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserDetailDO::getGender, "female");
        return userDetailMapper.selectCount(queryWrapper);
    }

    @Override
    public List<GroupUserDTO> getAddressGroupUserCount() {
        return userDetailMapper.selectList();
    }

    @Override
    public List<GroupFamilyDTO> getAddressGroupFamilyCount() {
        return familyMapper.selectList();
    }

    @Override
    public List<GroupGenderDTO> getGenderCount() {
        return  userDetailMapper.selectGenderCount();
    }

    @Override
    public List<GroupAgeDTO> getGroupAges() {
        return userDetailMapper.selectGroupAge();
    }

    @Override
    public List<GroupMonthDTO> getGroupMonths() {
        return userDetailMapper.getGroupMonths();
    }

    @Override
    public List<FamilySortDTO> getFamilyRank() {
        return familyMapper.getFamilyRank();
    }

}
